﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KlubSportowy.Models;

namespace KlubSportowy.Controllers
{
    public class ProfileController : jTableController
    {
        //
        // GET: /Profile/

        public ActionResult Index()
        {
            dbEntities db = new dbEntities();
            return View(db);
        }

        //
        // GET: /Profile/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Profile/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Profile/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddNews(FormCollection collection)
        {
            try
            {
                dbEntities db = new dbEntities();
                NewsSet ns = new NewsSet();
                DateTime dt = new DateTime();
                if (DateTime.TryParseExact(collection["date"], "dd/mm/yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dt))
                {
                    ns.Date = dt;
                }
                else ns.Date = DateTime.Now;
                ns.Text = HttpUtility.HtmlEncode(collection["elm1"]);
                ns.Id = -1;
                db.NewsSet.AddObject(ns);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [HttpPost]
        public ActionResult AddEvent(FormCollection collection)
        {
            try
            {
                dbEntities db = new dbEntities();
                ActivitiesSet acs = new ActivitiesSet();
                DateTime dt = new DateTime();
                if (DateTime.TryParseExact((collection["date"]), "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dt))
                {
                    acs.Date = dt;
                }
                else acs.Date = DateTime.Now;
                acs.Description = HttpUtility.HtmlEncode(collection["opis"]);
                acs.Id = -1;
                db.ActivitiesSet.AddObject(acs);
                db.SaveChanges();
                EventsInRoles eir = new EventsInRoles();
                foreach (string key in Request.Form.AllKeys)
                {
                    if (key.StartsWith("role"))
                    {
                        foreach (string guid in Request.Form[key].ToString().Split(','))
                        {
                        eir = new EventsInRoles();
                        eir.AcivityId = acs.Id;
                        eir.RoleId = new Guid(guid);
                        eir.Id = -1;
                        db.EventsInRoles.AddObject(eir);
                        db.SaveChanges();
                        }
                        
                    }
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Profile/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Profile/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Profile/Delete/5

        public ActionResult DeleteNews(int id)
        {
            dbEntities db = new dbEntities();
            db.NewsSet.DeleteObject(db.NewsSet.FirstOrDefault(x => x.Id == id));
            db.SaveChanges();
            return RedirectToAction("Index");

        }

        public ActionResult DeleteActivity(int id)
        {
            dbEntities db = new dbEntities();
            db.ActivitiesSet.DeleteObject(db.ActivitiesSet.FirstOrDefault(x => x.Id == id));
            db.SaveChanges();
            return RedirectToAction("Index");

        }

        //
        // POST: /Profile/Delete/5

        [HttpPost]
        public ActionResult DeleteNews(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
